package com.example.module.widget;

import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.EditText;

import com.example.R;
import com.example.entity.CustomAction;

public class CustomEditText extends EditText {
	private static final int FUZZ = 30;
	private static Drawable backDrawable, forardDrawablere, reloadDrawable, removeDrawable;
	private OnDrawableTouchListener onDrawableTouchListener;
	public Drawable leftDrawable, rightDrawable;
	
	public interface OnDrawableTouchListener {
		public boolean onDrawableTouch(MotionEvent event, CustomAction action);
	}

	public CustomEditText(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		initialize();
	}
	public CustomEditText(Context context, AttributeSet attrs) {
		super(context, attrs);
		initialize();
	}
	public CustomEditText(Context context) {
		super(context);
		initialize();
	}
	
	private void initialize() {
		backDrawable = getResources().getDrawable(R.drawable.arrowleft_35x36);
		forardDrawablere = getResources().getDrawable(R.drawable.arrowright_35x36);
		reloadDrawable = getResources().getDrawable(R.drawable.btn_add_reload_pressed);
		removeDrawable = getResources().getDrawable(R.drawable.btn_add_reload_cancel_pressed);
		leftDrawable = backDrawable;
		rightDrawable = forardDrawablere;
	}
	
	public OnDrawableTouchListener getOnDrawableTouchListener() {
		return onDrawableTouchListener;
	}
	public void setOnDrawableTouchListener(OnDrawableTouchListener onDrawableTouchListener) {
		this.onDrawableTouchListener = onDrawableTouchListener;
	}
	
	@Override
	public void setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom) {
		super.setCompoundDrawables(left, top, right, bottom);
	}
	
	@Override
	protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
		Log.e("CustomEditText onFocusChanged", "focused=" + focused + " direction=" + direction);
		
		if (focused) {
			leftDrawable = reloadDrawable;
			if (leftDrawable == null)
				leftDrawable = getResources().getDrawable(R.drawable.btn_add_reload_normal);
				
			rightDrawable = removeDrawable;
			if (rightDrawable == null)
				rightDrawable = getResources().getDrawable(R.drawable.btn_add_reload_cancel_normal);
		}
		else {
			leftDrawable = backDrawable;
			if (leftDrawable == null)
				leftDrawable = getResources().getDrawable(R.drawable.btn_add_reload_pressed);
			
			rightDrawable = forardDrawablere;
			if (rightDrawable == null)
				rightDrawable = getResources().getDrawable(R.drawable.btn_add_reload_cancel_pressed);
		}
		
		setCompoundDrawablesWithIntrinsicBounds(leftDrawable, null, rightDrawable, null);
		
		super.onFocusChanged(focused, direction, previouslyFocusedRect);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
//		Log.e("CustomEditText onTouchEvent", "event=" + event);
		
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			int x = (int) event.getX();
	        int y = (int) event.getY();
			CustomAction action = null;
			
			if (action == null && leftDrawable == backDrawable)
				if (checkLeftDrawablebounds(x, y))
					action = CustomAction.GO_BACK;
			
			if (action == null && leftDrawable == reloadDrawable)
				if (checkLeftDrawablebounds(x, y))
					action = CustomAction.RELOAD_URL;
			
			if (action == null && rightDrawable == forardDrawablere)
				if (checkRightDrawablebounds(x, y))
					action = CustomAction.GO_FORWARD;
			
			if (action == null && rightDrawable == removeDrawable)
				if (checkRightDrawablebounds(x, y))
					action = CustomAction.REMOVE_URL;
			
			Log.e("CustomEditText onTouchEvent", "action=" + action);
			
			if (action != null && onDrawableTouchListener != null)
				 return onDrawableTouchListener.onDrawableTouch(event, action);
        }
		
		return super.onTouchEvent(event);
	}
	
	private boolean checkLeftDrawablebounds(final int x, final int y) {
        final Rect bounds = leftDrawable.getBounds();
        return (x >= (getLeft() + getPaddingLeft() - FUZZ)
        		&& x <= (getLeft() + getPaddingLeft() + bounds.width() + FUZZ)
                && y >= (getPaddingTop() - FUZZ)
                && y <= (getHeight() - getPaddingBottom()) + FUZZ);
	}
	
	private boolean checkRightDrawablebounds(final int x, final int y) {
        final Rect bounds = rightDrawable.getBounds();
        return (x >= (getRight() - bounds.width() - FUZZ)
        		&& x <= (getRight() - getPaddingRight() + FUZZ)
                && y >= (getPaddingTop() - FUZZ)
                && y <= (getHeight() - getPaddingBottom()) + FUZZ);
	}

	@Override
	protected void finalize() throws Throwable {
		leftDrawable = null;
		rightDrawable = null;
		backDrawable = null;
		forardDrawablere = null;
		reloadDrawable = null;
		removeDrawable= null;
		super.finalize();
	}

//	private View progressBar;
//	@Override
//	protected void onDraw(Canvas canvas) {
//		Log.e("CustomEditText onDraw", "width=" + getWidth() + " height=" + getHeight());
//		
//		if (progressBar == null) {
//			progressBar = new View(getContext());
//			FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(10, getHeight(), Gravity.LEFT | Gravity.CENTER_VERTICAL);
//			FrameLayout.LayoutParams mylayoutParams = (FrameLayout.LayoutParams) getLayoutParams();
//			Log.e("CustomEditText onDraw", "leftMargin=" + mylayoutParams.leftMargin + " rightMargin=" + mylayoutParams.rightMargin);
//			layoutParams.setMargins(0, 0, mylayoutParams.leftMargin+ mylayoutParams.rightMargin, 0);
//			progressBar.setLayoutParams(layoutParams);
//			progressBar.setBackgroundColor(Color.argb(60, 27, 194, 37));
//			progressBar.setVisibility(View.VISIBLE);
//			FrameLayout parent = (FrameLayout) getParent();
//			parent.addView(progressBar);
//		}
//		
//		super.onDraw(canvas);
//	}
//	public View getProgressBar() {
//		return progressBar;
//	}
//	public void setProgressBar(View progressBar) {
//		this.progressBar = progressBar;
//	}
}
